package cn.gson.crm.control;




import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;


import cn.gson.crm.model.pojo.Product;
import cn.gson.crm.service.ProductService;
import cn.gson.crm.vo.ClassifyProductTbVo;
import cn.gson.crm.vo.ProductVo;

@Controller
@RequestMapping
public class ProductController {
	
	@Autowired
	ProductService prose;
	
	@RequestMapping
	public String test1() {
	
		return "test";
	}
	
	@RequestMapping("/tomain")
	public String test2(){
		return"xp/main";
	}
	@RequestMapping("/topro")
	public String test3(){
		return"xp/product";
	}
	@GetMapping("/get_all_pros")
	@ResponseBody
public List<ProductVo> getAllproducts(){
		return  prose.getAll();
	
	}
	@GetMapping("/get_all_pros_page")
	@ResponseBody
	public Map<Object, Object> getAllproductsPage(int page,int rows){
		List<ProductVo> list=prose.getAllProsBypage(page, rows);
		long count=prose.count();
	Map<Object, Object> map=new HashMap<>();
	map.put("total", count);
	map.put("rows", list);
		return map;
	}
	
	@GetMapping("/get_all_pros_page_where")
	@ResponseBody
	public Map<Object, Object> getAllproductsPagewhere(int page,int rows,String key,Long key1){
		Map<Object, Object> map=new HashMap<>();
		if(key1==null){
		List<ProductVo>list=prose.getAllProsBypageWhere(page, rows, key);
		long count=prose.countwhere(key);
		map.put("total", count);
		map.put("rows", list);}
		else{	
			List<ProductVo>list=prose.getAllProsBypageWhere1(key,key1);
			long count=prose.countwhere(key);
			map.put("total", count);
			map.put("rows", list);
		}
		return map;
	}
	
	
	@PostMapping("/get_all_pros_page_where1")
	@ResponseBody
	public List<ProductVo> getAllproductsPagewherein(Product poo,String proSta33){
	
		return prose.findsomepro(poo,proSta33);

	}
	@PostMapping("/deletpro")
	@ResponseBody
	@Transactional
	public void getAllproductsPagedel(HttpServletRequest reg){
		String [] myidd=reg.getParameterValues("myids[]");
	for(int i=0;i<myidd.length;i++){
	prose.delpro(myidd[i]);
	}
	}
	
	@PostMapping("/proadd")
	@ResponseBody
	@Transactional
	public void dddd(Product va,String proSta1,String classiyf11){
		prose.addNewPro(va, proSta1,classiyf11);
	}
	
	
	@PostMapping("/uptpro")
	@ResponseBody
	@Transactional
	public void pppp(Product vp,String proSta12,String classiyf111){


System.out.println(proSta12);

prose.uptproduct(vp,proSta12,classiyf111); 
	}
	
	@GetMapping("/toadd_product")
	public String toaddproduct(){
		return "xp/add_product";
	}
	@GetMapping("/toupt_product")
	public String uptaddproduct(){
		return "xp/upt_product";
	}
	@GetMapping("/get_all_classify")
	@ResponseBody 
	public List<ClassifyProductTbVo> getAllclassf(){
		return prose.getAllClassify();
	}
	@GetMapping("/toLookup")
public String test4(){
		return "xp/Lookup";
	}
	@GetMapping("/toClassify")
	public String toClassList(){
		return "xp/Classify_list";
	}
	
}
